//
//  RokumoteViewController.m
//  Rokumote
//
//  Created by John Roark on 10/6/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

/*
 Key: 5rksy355rjmwy2bxccpzkggu
 
 Application:	Rokumote
 Key:			5rksy355rjmwy2bxccpzkggu 
 Shared Secret:	Fh6xyMYHVj 
 Status:		active 
 Registered:	2 seconds ago 
 
 Rate Limits
 4	Queries per second
 5,000	Queries per day
 */

#import "RokumoteViewController.h"
#import "RokuPlayer.h"

#include <arpa/inet.h>

//extern int send_command (const char *server, const char *pszCommand);
extern unsigned long find_roku ();

/*RokumoteViewController *rView = nil;

void update_progress (unsigned long ip, int p)
{
	if (rView) {
		[rView updateStatus: p];
	}
}*/

@implementation RokumoteViewController

@synthesize	remote;
@synthesize configuration;

@synthesize	homeButton;
@synthesize	upButton;
@synthesize	rightButton;
@synthesize	leftButton;
@synthesize	downButton;
@synthesize	selectButton;
@synthesize	forwardButton;
@synthesize	rewindButton;
@synthesize	playButton;
@synthesize	infoButton;
@synthesize player;

// configuration view widgets
@synthesize	ipAddress;
@synthesize applyButton;
@synthesize	testButton;
@synthesize	cancelButton;
@synthesize	statusLabel;

@synthesize	searchSpinner;
//@synthesize	sendSpinner;

// debug widget
@synthesize label;

/*- (void) updateStatus: (int) p {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	[self performSelectorOnMainThread:@selector(updateProgress) withObject: nil waitUntilDone:NO];
	
	//[statusLabel setText:[[NSString alloc] initWithCString:inet_ntoa(addr)]];
	[pool release];
}*/

- (void) FindRoku: (RokumoteViewController *) obj {
	struct in_addr	addr;

	addr.s_addr = find_roku();
	
	[searchSpinner stopAnimating];
	
	if (addr.s_addr)
		[ipAddress setText:[[NSString alloc] initWithCString:inet_ntoa(addr)]];
	
	testButton.enabled = YES;
}

- (void) SendCommand: (NSString *) str {
	//[sendSpinner startAnimating];
	//self.view.enabled = NO;
	[player sendCommand:str];
	//self.view.enabled = YES;
	//[sendSpinner stopAnimating];
}

- (IBAction) pressButton: (id) sender {
	if (sender == homeButton) {
		//
		[label setText:@"Home"];
		//send_command ("press home");
		[self performSelectorInBackground:@selector(SendCommand:) withObject:@"press home"];
	} else if (sender == playButton) {
		//
		[label setText:@"Play"];
		//send_command ("press play");
		[self performSelectorInBackground:@selector(SendCommand:) withObject:@"press play"];
	} else if (sender == upButton) {
		//
		[label setText:@"Up"];
		//send_command ("press up");
		[self performSelectorInBackground:@selector(SendCommand:) withObject:@"press up"];
	} else if (sender == rightButton) {
		//
		[label setText:@"Right"];
		//send_command ("press right");
		[self performSelectorInBackground:@selector(SendCommand:) withObject:@"press right"];
	} else if (sender == leftButton) {
		//
		[label setText:@"Left"];
		//send_command ("press left");
		[self performSelectorInBackground:@selector(SendCommand:) withObject:@"press left"];
	} else if (sender == downButton) {
		//
		[label setText:@"Down"];
		//send_command ("press down");
		[self performSelectorInBackground:@selector(SendCommand:) withObject:@"press down"];
	} else if (sender == forwardButton) {
		//
		[label setText:@"Forward"];
		//send_command ("press fwd");
		[self performSelectorInBackground:@selector(SendCommand:) withObject:@"press forward"];
	} else if (sender == rewindButton) {
		//
		[label setText:@"Rewind"];
		//send_command ("press rwd");
		[self performSelectorInBackground:@selector(SendCommand:) withObject:@"press back"];
	} else if (sender == selectButton) {
		//
		[label setText:@"Select"];
		//send_command ("press select");
		[self performSelectorInBackground:@selector(SendCommand:) withObject:@"press select"];
	} else if (sender == infoButton) {
		// show info/config page
		[label setText:@"Info/Configuration"];
		self.view = self.configuration;
	} else if (sender == applyButton) {
		[player setIpAddr: [ipAddress text]];
		
		NSMutableDictionary	*dictionary;	
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString *documentsDirectory = [paths objectAtIndex:0];
		NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Settings.plist"];
		
		dictionary = [[NSMutableDictionary alloc] init];
		        
        [dictionary setObject:[ipAddress text] forKey:@"RokuIPAddress"];
        [dictionary writeToFile:path atomically: YES]; 
		
		[dictionary release];
		// show remote page
		self.view = self.remote;
	} else if (sender == cancelButton) {
		// do nothing, just switch views
		self.view = self.remote;
	} else if (sender == testButton) {
		
		[searchSpinner startAnimating];
		testButton.enabled = NO;

		[self performSelectorInBackground:@selector(FindRoku:) withObject:nil];
	} else {
	}

}

- (id)initWithCoder:(NSCoder *)aDecoder {
	if (self = [super initWithCoder:aDecoder]) {
		//rView = self;
	}
	return self;
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	
	if ([[player ipAddr] length] == 0)
		self.view = self.configuration;
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
   [super viewDidLoad];
	
	NSDictionary	*dictionary;	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Settings.plist"];
	
	dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
	
	NSString *ip = [dictionary objectForKey: @"RokuIPAddress"];

	player = [RokuPlayer createPlayerWithIP: ip];
	[ipAddress setText:ip];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
	
	[remote release];
	[configuration release];
	
	[homeButton release];
	[upButton release];
	[rightButton release];
	[leftButton release];
	[downButton release];
	[selectButton release];
	[forwardButton release];
	[rewindButton release];
	[playButton release];
	[infoButton release];
	[applyButton release];
	[testButton release];
	[cancelButton release];
	[ipAddress release];
	[searchSpinner release];
	[player release];
	
	[label release];
}

@end
